package com.adapters.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

import com.objects.GroupMember;

public class GroupMemberTableAdapter {
	public static final String TABLE_NAME = ContentProviderDB.PREFIX
			+ "group_members";
	public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + ContentProviderDB.COL_ID
			+ " integer primary key, " + ContentProviderDB.COL_FIRST_NAME
			+ " text, " + ContentProviderDB.COL_LAST_NAME + " text, "
			+ ContentProviderDB.COL_GROUP_ID + " integer not null, "
			+ ContentProviderDB.COL_EMAIL + " text, "
			+ ContentProviderDB.COL_ORDER + " integer not null)";

	private Context mContext;

	public GroupMemberTableAdapter(Context context) {
		mContext = context;
	}

	public void add(GroupMember item, long groupID, int order) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		ContentValues values = new ContentValues();
		values.put(ContentProviderDB.COL_ID, item.getID());
		values.put(ContentProviderDB.COL_GROUP_ID, groupID);
		values.put(ContentProviderDB.COL_FIRST_NAME, item.getFirstName());
		values.put(ContentProviderDB.COL_LAST_NAME, item.getLastName());
		values.put(ContentProviderDB.COL_EMAIL, item.getEmail());
		values.put(ContentProviderDB.COL_ORDER, order);
		mContext.getContentResolver().insert(contentUri, values);

	}

	public boolean clear() {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int value = mContext.getContentResolver()
				.delete(contentUri, null, null);
		return value > 0;

	}

	public boolean clear(long groupID) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int value = mContext.getContentResolver().delete(contentUri,
				ContentProviderDB.COL_GROUP_ID + "=" + groupID, null);
		return value > 0;

	}

	public ArrayList<GroupMember> getItems(long groupID) throws SQLException {
		ArrayList<GroupMember> results = new ArrayList<GroupMember>();
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor mCursor = mContext.getContentResolver().query(contentUri, null,
				ContentProviderDB.COL_GROUP_ID + "=" + groupID, null,
				ContentProviderDB.COL_ORDER + " ASC");
		if (mCursor.moveToFirst()) {
			do {
				GroupMember object = new GroupMember();
				object.setID(mCursor.getLong(mCursor
						.getColumnIndex(ContentProviderDB.COL_ID)));
				object.setFirstName(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_FIRST_NAME)));
				object.setLastName(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_LAST_NAME)));
				object.setEmail(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_EMAIL)));

				results.add(object);
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		return results;
	}

}
